import { checkPwdInit, getAccount } from "@src/api/member";
import router from "@src/router";
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import member from "@src/store/member";
import base from "@src/store/base";
import color from "@src/common/color";

Component({
  behaviors: [storeBindingsBehavior, router.routerBehavior],
  storeBindings: [{
    store: member,
    fields: ['isMember', 'memberName', 'phone'],
    actions: ['getBaseInfo'],
  }, {
    store: base,
    fields: ['themeColor', 'themeTitleColor', 'statusBarHeight', 'navBarHeight']
  }],
  data: {
    navHeight: 80,
    totalFreeBalance: 0,
    totalBalance: 0,
    availableBalance: 0,
    isPwdInit: true,
    loading: false,
    infoData: null,
  },
  observers: {
    'isMember': function (isMember) {
      if (isMember && this.data.infoData == null) {
        this.fetchData();
      } else if(isMember === false) {
        wx.nextTick(()=>{
          if(!this.data.isMember){
            wx.showModal({
              title: '您还不是会员哦！',
              content: '是否前往开卡页面',
              success(res) {
                if (res.confirm) {
                  router.push({name:"indexChooseCard"})
                } else if (res.cancel) {
                  wx.navigateBack()
                }
              }
            })
          }
        })
      }
    },
    'themeColor': function (themeColor) {
      if (!themeColor) {
        return;
      }
      this.setData({
        themeColor05: color.HexToRGB(themeColor, 50),
        themeColor0: color.HexToRGB(themeColor, 0),
      })
    }
  },
  methods: {
    onPullDownRefresh() {
      if (this.data.isMember) {
        this.fetchData();
      } else {
        wx.stopPullDownRefresh();
      }
    },
    onShow(){
      this.updateStoreBindings();
      if (this.data.isMember == null) {
        this.getBaseInfo()
      } else if(this.data.isMember == true){
        this.fetchData();
      }else {

      }
    },
    showInitPwdTips() {
      wx.showModal({
        title: "安全提示",
        content: "您正在使用会员充值，为保证账户余额安全，请务必设置支付密码配合使用！",
        confirmText: "设置密码",
        success: sm => {
          if (sm.confirm) {
            router.push({ name: "member/resetPassword", params: { type: "init" } })
          }
        }
      });
    },
    fetchData() {
      this.setData({ loading: true })
      checkPwdInit().then(res => {
        if (res.code === 0) {
          this.setData({ isPwdInit: res.data.initialed })
        }
      })
      getAccount().then(res => {
        wx.stopPullDownRefresh();
        this.setData({ loading: false })
        if (res.code === 0) {
          let infoData = res.data;
          let { balance, giftBalance, lockedBalance, lockedGiftBalance } = infoData
          let totalBalance = balance + giftBalance
          let totalFreeBalance = lockedBalance + lockedGiftBalance
          let availableBalance = totalBalance - totalFreeBalance
          this.setData({ infoData: res.data, totalBalance, totalFreeBalance, availableBalance })
        }
      }).catch(() => {
        wx.stopPullDownRefresh();
        this.setData({ loading: false })
      })
    },
    showFreezeDesc() {
      this.selectComponent("#freezeDescDialog").open();
    },
    handleToRecharge() {
      if (this.data.isPwdInit) {
        router.push({ name: "member/balanceRecharge" })
      } else {
        this.showInitPwdTips();
      }
    },
    handleResetPwd() {
      if (this.data.isPwdInit) {
        // 弹窗获取手机号
        let showPhoneContent = `重置支付密码需要验证你已绑定的手机 ${this.data.phone.substr(0, 3) + '****' + this.data.phone.substr(7)}`
        this.selectComponent("#get-phone").open({ text: showPhoneContent, auto: true }).then(phone => {
          if (phone !== this.data.phone) {
            wx.showToast({ title: "验证手机号失败，请重试", icon: "none", duration: 3000 });
            return;
          }
          router.push({
            name: "member/resetPassword",
            params: { type: "reset" }
          })
        })
      } else {
        // 初始密码
        router.push({ name: "member/resetPassword", params: { type: "init" } })
      }
    },
  }
})
